﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using Crimson;
using Crimson.Dao;
using StructureMap;


namespace Ornament.MVCWebFrame.Models
{
    /// <summary>
    /// 内容管理器。
    /// </summary>
    public static class InfoManager
    {
        public static IDaoFactory DaoFactory
        {
            get
            {
                
                return ObjectFactory.GetInstance<IDaoFactory>();
            }
        }

        public static IList<Info> GetInfos(string infoTypeName, int pageIndex, int pageSize, bool cascade)
        {
            IList<Info> result = DaoFactory.InfoDao.Find(infoTypeName, pageIndex, pageSize, cascade,
                                                         InfoState.Draft | InfoState.Published);
            foreach (Info info in result)
            {
                FillContent(info);
                Save(info);
            }
            return result;
        }

        /// <summary>
        /// 创建一个可编辑的信息.它会保存到数据库
        /// </summary>
        /// <returns></returns>
        public static Info CreateInfo(string publisher)
        {
            var result = new Info(publisher);
            DaoFactory.InfoDao.SaveOrUpdate(result);
            SaveContent(result.Id, result.Content);
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="info"></param>
        public static void Save(Info info)
        {
            if (info == null)
                throw new ArgumentNullException("info");
            DaoFactory.InfoDao.Save(info);
            SaveContent(info.Id, info.Content);
        }

        /// <summary>
        /// 获取Info信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Info Get(Guid id)
        {
            if (id == Guid.Empty)
                throw new ArgumentNullException("id", "id is " + Guid.Empty);
            var info = DaoFactory.InfoDao.Get(id);
            FillContent(info);
            return info;
        }

        /// <summary>
        /// 通过Id获取文章内容
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private static string GetContentFilePath(Guid id)
        {
            return GetDirectory(id) + "/" + id + ".txt";
        }

        /// <summary>
        /// 通过Id获取文章路径
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string GetDirectory(Guid id)
        {
            string path = Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, "Crimson");
            var info = new DirectoryInfo(Path.Combine(path, id.ToString()));
            if (!info.Exists)
                info.Create();
            return info.FullName;
        }
        private static void SaveContent(Guid id, string content)
        {
            var fileInfo = GetContentFilePath(id);

            using (var writer = new StreamWriter(File.OpenWrite(fileInfo)))
            {
                writer.Write(content);
            }
        }

        public static void FillContent(Info info)
        {
            var filePath = GetContentFilePath(info.Id);
            if (File.Exists(filePath))
            {
                using (var sr = new StreamReader(File.OpenRead(filePath)))
                {
                    info.Content = sr.ReadToEnd();
                }
            }
            else
            {
                info.Content = "内容已经遗失";
            }
        }
        /// <summary>
        /// 删除内容
        /// </summary>
        /// <param name="infoId"></param>
        public static void Delete(Guid infoId)
        {
            Info info = DaoFactory.InfoDao.Get(infoId);
            DaoFactory.InfoDao.Delete(info);

            if (Directory.Exists(GetDirectory(infoId)))
                Directory.Delete(GetDirectory(infoId), true);
        }
    }
}
